package com.xls.dormitoryservice.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xls.base.annotation.Log;
import com.xls.commonutils.Result;
import com.xls.base.model.Student;
import com.xls.dormitoryservice.service.StudentService;
import com.xls.dormitoryservice.vo.StudentQuery;
import com.xls.base.vo.StudentVo;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xls
 * @since 2021-03-23
 */
@RestController
@RequestMapping("/dormitory/student")
@Api(value = "学生信息管理")
@Slf4j
public class StudentController {

    @Autowired
    private StudentService studentService;

    @Log(title = "学生管理", businessType = "查询")
    @PreAuthorize("hasAnyAuthority('dormitory:student:list')")
    @GetMapping("/getStudent/{id}")
    public Result<Student> getStudent(@PathVariable String id) {
        return Result.success(studentService.getById(id));
    }

    @Log(title = "学生管理", businessType = "查询")
    @PreAuthorize("hasAnyAuthority('dormitory:student:list')")
    @PostMapping("/list/{page}/{limit}")
    public Result<Page<StudentVo>> list(@PathVariable Integer page, @PathVariable Integer limit,
                                        @RequestBody StudentQuery studentQuery) {
        return Result.success(studentService.getStudentPage(page, limit, studentQuery));
    }

    @Log(title = "学生管理", businessType = "添加")
    @PreAuthorize("hasAnyAuthority('dormitory:student:save')")
    @PostMapping("/save")
    public Result<Student> save(@RequestBody Student student) {
        return studentService.saveStudent(student) ? Result.success(student) : Result.error();
    }

    @Log(title = "学生管理", businessType = "修改")
    @PreAuthorize("hasAnyAuthority('dormitory:student:update')")
    @PostMapping("/update")
    public Result<Student> update(@RequestBody Student student) {
        return studentService.updateStudent(student) ? Result.success(student) : Result.error();
    }

    @Log(title = "学生管理", businessType = "删除")
    @PreAuthorize("hasAnyAuthority('dormitory:student:remove')")
    @DeleteMapping("/remove/{id}")
    public Result<Student> remove(@PathVariable String id) {
        return studentService.removeById(id) ? Result.success() : Result.error();
    }

}

